package org.gcu.filestorage.controller;

import org.gcu.filestorage.api.ErrorCode;
import org.gcu.filestorage.pojo.AccountInfo;
import org.gcu.filestorage.pojo.RequestResult;
import org.gcu.filestorage.serive.AccountInfoService;
import org.gcu.filestorage.uitls.MD5Util;
import org.gcu.filestorage.uitls.TokenUtil;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.Objects;

@RestController
public class LoginController {

    private final AccountInfoService accountInfoService;

    @Autowired
    public LoginController(AccountInfoService accountInfoService) {
        this.accountInfoService = accountInfoService;
    }

    @PostMapping("/login")
    public RequestResult<String> login(@RequestParam("account") String account, @RequestParam("password") String password) throws JSONException, FileNotFoundException {
        AccountInfo accountInfo = accountInfoService.queryAccountInfo(account);
        RequestResult<String> requestResult = new RequestResult<>();
        if (accountInfo == null) {
            requestResult.setData("");
            requestResult.setResCode(ErrorCode.ACCOUNT_NOT_EXISTED);
            requestResult.setMessage("账号不存在");
            return requestResult;
        }
        if (accountInfo.getRegisterStatus() != 2)  {
            requestResult.setData("");
            requestResult.setResCode(ErrorCode.ACCOUNT_REGISTER_WAITING_APPROVAL);
            requestResult.setMessage("账号注册尚未通过审批");
            return requestResult;
        }
        String encryptedPassword = MD5Util.encryptByMD5(password);
        if (!Objects.equals(accountInfo.getPassword(), encryptedPassword)) {
            requestResult.setData("");
            requestResult.setResCode(ErrorCode.PASSWORD_INCORRECT);
            requestResult.setMessage("密码不正确");
            return requestResult;
        }
        String token = TokenUtil.genToken(account);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("TOKEN", token);
        jsonObject.put("MENU_LIST", getMenuList(accountInfo));
        requestResult.setData(jsonObject.toString());
        requestResult.setResCode(ErrorCode.SUCCESS);
        requestResult.setMessage("登录成功");
        return requestResult;
    }

    private String getMenuList(AccountInfo accountInfo) throws FileNotFoundException {
        String resourceLocation = accountInfo.getAccountRole() == 0 ? "classpath:admin_menu.json" : "classpath:user_menu.json";
        File adminFile = ResourceUtils.getFile(resourceLocation);
        try(BufferedInputStream in = new BufferedInputStream(new FileInputStream(adminFile))) {
            byte[] content = in.readAllBytes();
            return new String(content);
        } catch (IOException e) {
            return "";
        }
    }
}
